var slidint;

function newTimer() {
    stopit();
    var today;
    today = new Date();
    var str = today.toLocaleDateString();
    str += "  " + week();
    str += "  " + today.toLocaleTimeString();
    var o = document.getElementById("DateTime");
    o.innerHTML = str;
    slidint = setTimeout(newTimer, 1000);
}

function stopit() {
    clearTimeout(slidint);
}

function week() {
    var d, day, x, s = " ";
    var x = new Array("星期日", "星期一", "星期二");
    var x = x.concat("星期三", "星期四", "星期五");
    var x = x.concat("星期六");
    d = new Date();
    day = d.getDay();
    return (s += x[day]);
}

window.onload = function () {
    newTimer();
};


layui.use('element', function () {
    var element = layui.element;
});


// // 数据大屏10分钟刷新一次
$(function () {
    setInterval(function () {
        // window.location.reload();
        warn();
        warn1();
        warn2();
        warn3();

        warn4();
        warn5();
        warn6();
        warn7();
        warn8();
        warn9();
        warn10();
        warn11();
        warn12();
    }, 1000);
});
//
// $(function () {
//    setInterval(function () {
//        parent.location.reload();
//    }, 30000);
// });
// TODO  如何动态刷新
function warn() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['warn_sum'];
                var html1 = template('new_warn', {'data': data});
                document.getElementById('news-warn').innerHTML = html1;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn1() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data2 = result['data']['warn_sum'];
                var html2 = template('warn_count', {'data': data2});
                document.getElementById('warns_count_ly').innerHTML = html2;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn2() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data2 = result['data']['most_count'];
                var html3 = template('zj_count', {'data': data2});
                document.getElementById('zjs_count').innerHTML = html3;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn3() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['second_count'];
                var html4 = template('dsj_count', {'data': data});
                document.getElementById('dsjs_count').innerHTML = html4;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn4() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['first_type'];
                var html5 = template('type1', {'data': data});
                document.getElementById('first_types').innerHTML = html5;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn5() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['second_type'];
                var html6 = template('type2', {'data': data});
                document.getElementById('second_types').innerHTML = html6;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn6() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['warn_sum'];
                var html7 = template('count2', {'data': data});
                document.getElementById('warns_count_middle').innerHTML = html7;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn7() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['first_type'];
                var html8= template('type1_middle', {'data': data});
                document.getElementById('first_type_middle').innerHTML = html8;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn8() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['warn_sum'];
                var html9= template('count3', {'data': data});
                document.getElementById('db_count').innerHTML = html9;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn9() {
    $.ajax({
        type: 'get',
        url: '/public/flush_image/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']["image"];
                data = "http://127.0.0.1:8000"+data
                // console.log("图片地址",data)
                var html10= template('image', {'data': data});
                document.getElementById('image_new').innerHTML = html10;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn10() {
    $.ajax({
        type: 'get',
        url: '/public/three/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']
                var html11= template('three', {'data': data});

                document.getElementById('new_three').innerHTML = html11;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn11() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['first_type'];
                var html12 = template('firsttype1', {'data': data});
                document.getElementById('first_type1').innerHTML = html12;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
function warn12() {
    $.ajax({
        type: 'get',
        url: '/public/flush/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data']['second_type'];
                var html13 = template('secondtype1', {'data': data});
                document.getElementById('second1').innerHTML = html13;
            } else {
                return
            }
        },
        error: function () {
            return
        }
    });
}
// 定时异步刷新大屏右下角最新预警信息
$(function () {
    warn();
    // setInterval(function () {
    //     warn();
    //     var NewsWarn = $('#news-warn');
    //     var NewsImage = $('#new-images');
    //     NewsWarn.empty(); // 再次刷新前 先移除dom
    //     NewsImage.empty(); // 再次刷新前 先移除dom
    // }, 10000);
});
// TODO  如何动态刷新
// function warn() {
//     $.ajax({
//         type: 'get',
//         url: '/public/index_warn/',
//         data: {},
//         success: function (result) {
//             if (result['code'] === 200) {
//                 var data = result['data'];
//                 var html1 = template('new-warn', {'data': data});
//                 document.getElementById('news-warn').innerHTML = html1;
//             } else {
//                 return
//             }
//         },
//         error: function () {
//             return
//         }
//     });
// }

// 模板过滤器
$(function () {
    if (window.template) {
        template.defaults.imports.newTime = function (dateValue) {
            var new_times = dateValue.slice(0, 14);
            return new_times
        }
    }
});
// $(function () {
//     if (window.template) {
//         template.defaults.imports.newTime = function (dateValue) {
//             var new_times = dateValue.slice(6, 14);
//             return new_times
//         }
//     }
// });

// 高发监控区域统计

// *****************************************************************

$(function () {
    var myChart = echarts.init(document.getElementById('echarts-line'));
    $.ajax({
        type: 'get',
        url: '/public/line_top/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data'];
                console.log(data);
                var name_data = [];
                var sum_data = [];
                var date_data = [];
                for (var i = 0; i < data.length; i++) {
                    for (var key in data[i]) {
                        name_data.push(key);
                        sum_data.push(data[i][key]);
                    }
                }
                myChart.setOption(
                    option = {
                        tooltip: {
                            trigger: 'axis',
                            axisPointer: {
                                type: 'cross',
                                label: {
                                    backgroundColor: '#6a7985'
                                },
                            }
                        },
                        legend: {
                            data: name_data,
                            textStyle: {
                                color: '#AAAEAA',  // 图例文字颜色
                                fontSize: '10',
                            }
                        },
                        toolbox: {
                            // feature: {
                            //     saveAsImage: {}
                            // }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        xAxis: [
                            {
                                type: 'category',
                                boundaryGap: false,
                                data: data[3].reverse(),
                                axisLine: {
                                    lineStyle: {
                                        color: '#AAAEAA',
                                    }
                                }
                            }
                        ],
                        yAxis: [
                            {
                                type: 'value',
                                splitLine: {
                                    show: true,
                                    lineStyle: {
                                        color: '#3A4767',
                                        width: '1',
                                        type: 'solid'
                                    }
                                },
                                axisLine: {
                                    lineStyle: {
                                        color: '#AAAEAA',
                                    }
                                }
                            }
                        ],
                        series: [
                            {
                                name: name_data[0],
                                type: 'line',
                                stack: '总量',
                                areaStyle: {},
                                data: sum_data[0].reverse()
                            },
                            {
                                name: name_data[1],
                                type: 'line',
                                stack: '总量',
                                areaStyle: {},
                                data: sum_data[1].reverse()
                            },
                            {
                                name: name_data[2],
                                type: 'line',
                                stack: '总量',
                                areaStyle: {},
                                data: sum_data[2].reverse()
                            },
                        ],
                        color: ['#27F47C', '#279CF4', '#FBC224']
                    }
                )
            }
        }
    });
});

// *****************************************************************

// 本周操作统计

// *****************************************************************

$(function () {
    var myChart = echarts.init(document.getElementById('cols'));
    $.ajax({
        type: 'get',
        url: '/public/line_data/',
        data: {
            'index': 0
        },
        success: function (result) {
            if (result['code'] === 200) {
                var data = result['data'];
                myChart.setOption(
                    {
                        legend: {
                            textStyle: {
                                color: '#AAAEAA',  // 图例文字颜色
                                fontSize: '10',
                            }
                        },
                        tooltip: {},
                        dataset: {
                            // 这里指定了维度名的顺序，从而可以利用默认的维度到坐标轴的映射。
                            // 如果不指定 dimensions，也可以通过指定 series.encode 完成映射，参见后文。
                            dimensions: ['product', '已完成', '已丢弃'],
                            source: data
                        },
                        xAxis: {
                            type: 'category',
                            axisLine: {
                                lineStyle: {
                                    color: '#AAAEAA',
                                }
                            }
                        },
                        yAxis: {
                            splitLine: {
                                show: true,
                                lineStyle: {
                                    color: '#3A4767',
                                    width: '1',
                                    type: 'solid'
                                }
                            },
                            axisLine: {
                                lineStyle: {
                                    color: '#AAAEAA',
                                }
                            }
                        },
                        grid: {
                            left: '2%',
                            right: '2%',
                            bottom: '2%',
                            containLabel: true
                        },
                        series: [{
                            type: 'bar'
                        },
                            {
                                type: 'bar'
                            }
                        ],
                        color: ['#27F47C', '#279CF4']
                    }
                )
            }
        }
    });
});

// 地图

// *****************************************************************

var styleJson = [{
    "featureType": "land",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on",
        "color": "#19264A"
    }
}, {
    "featureType": "water",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on",
        "color": "#19264A",
    }
}, {
    "featureType": "green",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on",
        "color": "#19264A"
    }
}, {
    "featureType": "building",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "building",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "building",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "subwaystation",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off",
        "color": "#557DB1"
    }
}, {
    "featureType": "education",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off",
        "color": "#e4f1f1ff"
    }
}, {
    "featureType": "medical",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off",
        "color": "#f0dedeff"
    }
}, {
    "featureType": "scenicspots",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off",
        "color": "#4c61ffff"
    }
}, {
    "featureType": "highway",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "highway",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#557DB1"
    }
}, {
    "featureType": "highway",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "highway",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "highway",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#759879ff"
    }
}, {
    "featureType": "highway",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "highway",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "arterial",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "arterial",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#557DB1"
    }
}, {
    "featureType": "arterial",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#554631fa"
    }
}, {
    "featureType": "arterial",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "arterial",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#759879ff"
    }
}, {
    "featureType": "arterial",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "local",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "local",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "local",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#ffffff00"
    }
}, {
    "featureType": "local",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "local",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#979c9aff"
    }
}, {
    "featureType": "local",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#ffffffff"
    }
}, {
    "featureType": "railway",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "railway",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "railway",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "subway",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "subway",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#d8d8d8ff"
    }
}, {
    "featureType": "subway",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#ffffff00"
    }
}, {
    "featureType": "subway",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "subway",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#979c9aff"
    }
}, {
    "featureType": "subway",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#ffffffff"
    }
}, {
    "featureType": "continent",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "continent",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "continent",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#0e4dffff"
    }
}, {
    "featureType": "continent",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#ffcb7cff"
    }
}, {
    "featureType": "city",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "city",
    "elementType": "labels",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "city",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#557DB1",
        "weight": 50
    }
}, {
    "featureType": "city",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#19264A",
        "weight": 3
    }
}, {
    "featureType": "town",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "town",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "town",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "town",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#ffffffff"
    }
}, {
    "featureType": "road",
    "elementType": "geometry.fill",
    "stylers": {
        "color": "#557DB1"
    }
}, {
    "featureType": "poilabel",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "districtlabel",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "road",
    "elementType": "geometry",
    "stylers": {
        "visibility": "on"
    }
}, {
    "featureType": "road",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "road",
    "elementType": "geometry.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "district",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "poilabel",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "poilabel",
    "elementType": "labels.text.fill",
    "stylers": {
        "color": "#557DB1"
    }
}, {
    "featureType": "poilabel",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#19264A",
        "weight": 3
    }
}, {
    "featureType": "manmade",
    "elementType": "geometry",
    "stylers": {
        "color": "#19264A",
        "visibility": "on"
    }
}, {
    "featureType": "districtlabel",
    "elementType": "labels.text.stroke",
    "stylers": {
        "color": "#19264A"
    }
}, {
    "featureType": "entertainment",
    "elementType": "geometry",
    "stylers": {
        "color": "#ffffffff",
        "visibility": "off"
    }
}, {
    "featureType": "shopping",
    "elementType": "geometry",
    "stylers": {
        "color": "#19264A",
        "visibility": "off"
    }
}, {
    "featureType": "estate",
    "elementType": "geometry",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "highwaysign",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "nationalwaysign",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "provincialwaysign",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "tertiarywaysign",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "subwaylabel",
    "elementType": "labels.icon",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "subwaylabel",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "tertiarywaysign",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "provincialwaysign",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "nationalwaysign",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}, {
    "featureType": "highwaysign",
    "elementType": "labels",
    "stylers": {
        "visibility": "off"
    }
}];

$(function () {
    $.ajax({
        type: 'get',
        url: '/public/bdmap/',
        data: {},
        success: function (result) {
            if (result['code'] === 200) {

                var data = result['data'];
                // 百度地图API功能
                var map = new BMap.Map("map"); // 创建Map实例
                //添加地图类型控件
                map.centerAndZoom(new BMap.Point(120.246905, 30.246307), 20); //  初始化地图,设置中心点坐标和地图级别
                map.enableScrollWheelZoom(true); // 设置鼠标滚轮缩放
                map.setMapStyle({styleJson: styleJson});

                var navigationControl = new BMap.NavigationControl({
                    // 靠左上角位置
                    anchor: BMAP_ANCHOR_TOP_LEFT,
                    // LARGE类型
                    type: BMAP_NAVIGATION_CONTROL_LARGE,
                    // 启用显示定位
                    enableGeolocation: true
                });
                map.addControl(navigationControl); // 设置左上角控件//开启鼠标滚轮缩放

                var point = []; // 存放标注点经纬度数组
                var marker = []; // 存放标注点对象的数组

                // 遍历经纬度数组
                for (var i = 0; i < data.length; i++) {
                    point[i] = new BMap.Point(data[i]['longitude'], data[i]['latitude']); //　循环生成新的地图点
                    var myIcon = new BMap.Icon('/static/images/摄像头.png', new BMap.Size(50, 50)); // 创建监控点新图标(已启用)
                    marker[i] = new BMap.Marker(point[i], {
                        icon: myIcon
                    });
                    map.addOverlay(marker[i]); // 按照地图点坐标生成标记
                }
            } else {
                alert('获取失败...');
            }
        }
    });
});

// *****************************************************************